package cn.itcast.nems.journal.scheduler;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.itcast.nems.studentrecord.dao.entity.JournalTransferBatchImport;
import cn.itcast.nems.studentrecord.dao.mapper.JournalTransferBatchImportMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 转班凭单批量导入 服务实现类
 * </p>
 *
 * @author czq
 * @since 2024-03-13
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class JournalTransferBatchImportServiceImpl extends ServiceImpl<JournalTransferBatchImportMapper, JournalTransferBatchImport> implements JournalTransferBatchImportService {
    @Override
    public JournalTransferBatchImport getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(JournalTransferBatchImport params) {
        // TODO 代码逻辑....
        int updateById = baseMapper.updateById(params);
        if (updateById == 1) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }


    @Override
    public Boolean update(Integer id, String processState, String processDescription, Integer processRetriesNumber, String journalId) {
        JournalTransferBatchImport byId = this.getById(id);
        if (byId == null) {
            return Boolean.FALSE;
        }

        if (StringUtils.hasText(byId.getProcessDescription()) && byId.getProcessDescription().length() > 250) {
                processDescription = processDescription.substring(0, 250);
        }
        
        return lambdaUpdate()
                .set(JournalTransferBatchImport::getProcessState, processState)
                .set(JournalTransferBatchImport::getProcessDescription, processDescription)
                .set(JournalTransferBatchImport::getProcessRetriesNumber, processRetriesNumber)
                .set(JournalTransferBatchImport::getProcessJournalId, journalId)
                .eq(JournalTransferBatchImport::getId, id).update();
    }

    @Override
    public Boolean update(Integer id, String processState, String processDescription) {
        return update(id, processState, processDescription, 1, "");
    }

    @Override
    public List<JournalTransferBatchImport> list(String journalType) {
        return lambdaQuery()
                .eq(StringUtils.hasText(journalType), JournalTransferBatchImport::getJournalType, journalType)
                .eq(JournalTransferBatchImport::getDeletedFlag, Boolean.FALSE)
                // 不是,已完成｜FINISHED
                .ne(JournalTransferBatchImport::getProcessState, "FINISHED")
                .list();
    }
}
